<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>

    <style>
        * {
            margin: 0;
            padding: 0;
            list-style: none;
        }

        .banner {
            width: 1200px;
            height: 600px;
            overflow: hidden;
            position: relative;
        }

        .box {
            width: 7200px;
            display: flex;
            position: absolute;
            left: 0;
        }

        .box div {
            width: 1200px;
            height: 600px;
            border: 5px black solid;
            box-sizing: border-box;
            font-size: 100px;
            line-height: 600px;
            text-align: center;
        }

        .dot {
            width: 1200px;
            display: flex;
            justify-content: center;
            position: absolute;
            bottom: 0;
        }

        .dot li {
            width: 50px;
            height: 50px;
            margin: 20px;
            border-radius: 50%;
            border: 5px black solid;
            box-sizing: border-box;

        }

        .show {
            background-color: grey;
        }

        .prev {
            height: 100px;
            font-size: 50px;
            line-height: 100px;
            position: absolute;
            left: 0;
            top: 48%;
        }

        .next {
            height: 100px;
            font-size: 50px;
            line-height: 100px;
            position: absolute;
            right: 0;
            top: 48%;
        }
    </style>
</head>

<body>


    <div class="banner">
        <div class="box">
            <div>1</div>
            <div>2</div>
            <div>3</div>
            <div>4</div>
            <div>5</div>
            <div>1</div>
        </div>
        <button class="prev">&lt;</button>
        <button class="next">&gt;</button>
        <ul class="dot">
            <li class="show"></li>
            <li></li>
            <li></li>
            <li></li>
            <li></li>
        </ul>
    </div>


    <script>
        class banner {
            constructor() {
                this.oBanner = document.querySelector('.banner');
                this.oBox = this.oBanner.querySelector('.box');
                this.oImg = this.oBanner.querySelectorAll('.box div');
                this.oPrev = this.oBanner.querySelector('.prev');
                this.oNext = this.oBanner.querySelector('.next');
                this.oDot = this.oBanner.querySelectorAll('.dot li');
                this.index = 0;
                this.t = null;
                this.autoPlay();
                this.click();
                this.focus()
            }
            // 自动播放
            autoPlay() {
                this.t = setInterval(() => {
                    this.index++;
                    this.oBox.style.transition = '.5s';
                    this.oBox.style.left = `${-1200 * this.index}px`
                    if (this.index === this.oImg.length - 1) {
                        setTimeout(() => {
                            this.index = 0;
                            this.oBox.style.transition = '0s';
                            this.oBox.style.left = 0;
                        }, 1000);

                    }
                    this.dot()
                }, 2000);

            }

            dot() {
                for (let i = 0; i < this.oDot.length; i++) {
                    this.oDot[i].classList.remove('show')

                }
                if (this.index === this.oDot.length) {
                    this.index = 0;
                }
                this.oDot[this.index].classList.add('show')
            }
            click() {
                this.oBanner.onclick = (e) => {
                    if (e.target.className === 'next') {
                        clearInterval(this.t)
                        this.oBox.style.transition = '0s';
                        this.index++
                        if (this.index === this.oImg.length - 1) {
                            this.index = 0;
                        }
                        this.oBox.style.left = `${-1200 * this.index}px`;
                        this.dot();
                        this.autoPlay()
                    }
                    if (e.target.className === 'prev') {
                        clearInterval(this.t)
                        this.oBox.style.transition = '0s';
                        this.index--
                        if (this.index === - 1) {
                            this.index = this.oImg.length - 2;
                        }
                        this.oBox.style.left = `${-1200 * this.index}px`;
                        this.dot();
                        this.autoPlay()
                    }
                    if (e.target.tagName === 'LI') {
                        clearInterval(this.t);
                        this.oBox.style.transition = '0s';
                        for (let i = 0; i < this.oDot.length; i++) {
                            this.oDot[i].index = i;
                        }
                        this.index = e.target.index;
                        this.oBox.style.left = `${-1200 * this.index}px`;
                        this.dot();
                        this.autoPlay()
                    }
                }
            }

            focus() {
                window.onblur = () => {
                    clearInterval(this.t);
                }
                window.onfocus = () => {
                    clearInterval(this.t);
                    this.autoPlay()
                }
            }
        }

        const a = new banner()
    </script>
</body>

</html>